package com.ruoyi.dev.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dev.domain.DevDatavBlock;
import com.ruoyi.dev.mapper.DevDatavBlockMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dev.mapper.DevDatavMapper;
import com.ruoyi.dev.domain.DevDatav;
import com.ruoyi.dev.service.IDevDatavService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 大屏Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-25
 */
@Service
public class DevDatavServiceImpl implements IDevDatavService 
{
    private static final Logger log = LoggerFactory.getLogger(DevDatavServiceImpl.class);

    @Autowired
    private DevDatavMapper devDatavMapper;
    @Autowired
    private DevDatavBlockMapper devDatavBlockMapper;

    /**
     * 查询大屏
     * 
     * @param id 大屏主键
     * @return 大屏
     */
    @Override
    public DevDatav selectDevDatavById(Long id)
    {
        return devDatavMapper.selectDevDatavById(id);
    }

    /**
     * 查询大屏列表
     * 
     * @param devDatav 大屏
     * @return 大屏
     */
    @Override
    public List<DevDatav> selectDevDatavList(DevDatav devDatav)
    {
        return devDatavMapper.selectDevDatavList(devDatav);
    }

    /**
     * 新增大屏
     * 
     * @param devDatav 大屏
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDevDatav(DevDatav devDatav)
    {
        DevDatav old = devDatavMapper.selectDevDatavByCode(devDatav.getCode());
        if(old != null){
            throw new ServiceException("大屏代码不能重复:"+devDatav.getCode());
        }
        devDatav.setCreateTime(DateUtils.getNowDate());
        int ret = devDatavMapper.insertDevDatav(devDatav);
        for(int i=0;i<devDatav.getBlocks();i++){
            DevDatavBlock datavBlock = new DevDatavBlock();
            datavBlock.setDvId(devDatav.getId());
            datavBlock.setDvIndex(i+1);
            datavBlock.setName("版块"+datavBlock.getDvIndex());
            datavBlock.setChartType("pie");
            datavBlock.setDsCode(devDatav.getCode()+"_"+datavBlock.getDvIndex());
            devDatavBlockMapper.insertDevDatavBlock(datavBlock);
        }
        return ret;
    }

    /**
     * 修改大屏
     * 
     * @param devDatav 大屏
     * @return 结果
     */
    @Override
    public int updateDevDatav(DevDatav devDatav)
    {
        DevDatav old = devDatavMapper.selectDevDatavByCode(devDatav.getCode());
        if(old != null && !old.getId().equals(devDatav.getId())){
            throw new ServiceException("大屏代码不能重复:"+devDatav.getCode());
        }
        devDatav.setUpdateTime(DateUtils.getNowDate());
        return devDatavMapper.updateDevDatav(devDatav);
    }

    /**
     * 批量删除大屏
     * 
     * @param ids 需要删除的大屏主键
     * @return 结果
     */
    @Override
    public int deleteDevDatavByIds(Long[] ids)
    {
        return devDatavMapper.deleteDevDatavByIds(ids);
    }

    /**
     * 删除大屏信息
     * 
     * @param id 大屏主键
     * @return 结果
     */
    @Override
    public int deleteDevDatavById(Long id)
    {
        return devDatavMapper.deleteDevDatavById(id);
    }


}
